package git.bankview.wl.report;

import java.util.*;
import java.text.*;
import java.sql.*;

import laya.semi.process.*;

import git.bankview.sl.db.*;
import git.bankview.wl.common.*;
import git.bankview.wl.bean.LogonBean;
import git.bankview.sl.report.ReportCommon;
import git.bankview.sl.report.SchdUtil;

import org.apache.log4j.Logger;

public class ReportSchdListActivity extends SqlQueryActivityBase
{
	private static Logger log = Logger.getLogger(ReportSchdListActivity.class);
   
	protected StringBuffer buildSqlBuffer(IProcessContext oCtx)
		throws Exception
	{
		String oTransid = (String)oCtx.getField("transid");
		
		StringBuffer oSql = new StringBuffer("SELECT");
		oSql.append("  se.schdid  ")	//c01
			.append(", se.transid ")	//c02
			.append(", se.schddate ")	//c03
			.append(", se.schdtype ")	//c04
			.append(", se.schdexport ")	//c05
			.append(", se.schdstatus ")	//c06
			.append(", se.exectime ")	//c07
			.append(", se.termtime ")	//c08
			.append(", se.execmsg ")	//c09
			.append(", se.execdetail ")	//c10
			.append(", gd.paramvalue grpdeptno")	//c11
			.append(", gs.paramvalue grpschddate")	//c12
			.append(", gu.paramvalue grpuserid")	//c13
			.append(", gc.paramvalue grpschduser")	//c14
			.append(", gn.paramvalue grpdeptnos")	//c15
			.append(" FROM dqSchdEntry se")
			.append(", outer dqschdparam gd, outer dqschdparam gs, outer dqschdparam gu, outer dqschdparam gc, outer dqschdparam gn")
			.append(" WHERE se.transid = '").append(oTransid).append("'")
			.append(" and se.schdstatus < ").append(ReportCommon.SCHD_STATUS_DELETED)
			.append(" and gd.paramname = 'GRP_DEPT_NO' and gd.schdid = se.schdid")
			.append(" and gs.paramname = 'GRP_SCHD_DATE' and gs.schdid = se.schdid")
			.append(" and gu.paramname = 'GRP_USER_ID' and gu.schdid = se.schdid")
			.append(" and gc.paramname = 'GRP_SCHD_USER' and gc.schdid = se.schdid")
			.append(" and gn.paramname = 'GRP_DEPT_NOS' and gn.schdid = se.schdid");
		
		return oSql;
	}
	
	public IObjectFilter createNewFilter(IProcessContext oCtx)
	{
		log.debug("CREATE QUERY FILTER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
		return new QueryFilter(oCtx);
	}
}

class QueryFilter
	implements IObjectFilter
{
	private static Logger log = Logger.getLogger(QueryFilter.class);
	
	private List m_oAllowedDeptList = null;
	private List m_oAllowedUserList = null;
	
	public QueryFilter(IProcessContext oCtx)
	{
		Connection oConn = null;
		try
		{
			oConn = DBConnectionFactory.getCoreConnection();
		}
		catch(Exception e)
		{
			throw new QueryFilterConstructionException("cannot get db connection: ", e);
		}
		
		try
		{
			LogonBean oLogon = (LogonBean)oCtx.getField("logon");
			String transid = (String)oCtx.getField("transid");
			String[] vPropVal = SchdUtil.getReportPropertiesByName(transid, "REPORT_DEPT_AUTH", oConn);
			if (0 < vPropVal.length)
			{
				m_oAllowedDeptList = SchdUtil.getAllowedDeptList(oLogon.getUsername());
				log.debug(m_oAllowedDeptList);
			}
			
			vPropVal = SchdUtil.getReportPropertiesByName(transid, "REPORT_USER_AUTH", oConn);
			if (0 < vPropVal.length)
			{
				m_oAllowedUserList = SchdUtil.getAllowedUserList(oLogon.getUsername());
				log.debug(m_oAllowedUserList);
			}
		
			if (null == m_oAllowedDeptList && null == m_oAllowedUserList)
				log.info("NO LIMIT REPORT: " + transid);
		}
		catch(Exception e)
		{
			throw new QueryFilterConstructionException("cannot get the login info", e);
		}
		finally
		{
			try
			{
				oConn.close();
			}
			catch(Exception e)
			{
				log.warn("connectino close exception: ", e);
			}
		}
	}
	
	public boolean doFilter(Object obj)
	{
		log.debug("doFilter");
		Map rec = (Map)obj;
		if (null != m_oAllowedDeptList)
		{
			String oDeptNo = (String)rec.get("grpdeptno");
			
			if (null == oDeptNo || !m_oAllowedDeptList.contains(oDeptNo.trim()))
			{
				String oDeptNos = (String)rec.get("grpdeptnos");
				if (null == oDeptNos)
					return false;
				
				String[] vDeptNo = oDeptNos.split(ReportCommon.GRP_PARAMS_SPLITER_REGEX);
				for (int i = 0; i < vDeptNo.length; ++i)
				{
					if (!m_oAllowedDeptList.contains(vDeptNo[i].trim()))
						return false;
				}
			}
		}
		
		if (null != m_oAllowedUserList)
		{
			if (!m_oAllowedUserList.contains(((String)rec.get("grpuserid")).trim()))
				return false;
		}
		
		return true;
	}
}
